#!/usr/bin/python
# -*- coding: UTF-8 -*-

from abc import ABCMeta, abstractmethod

from mysqlmoudel.sqlmoudle import sqlmoudle

import ConfigParser


class Builder:
    __metaclass__ = ABCMeta

    @abstractmethod
    def build_server_address(self):
        pass

    @abstractmethod
    def build_username(self):
        pass

    @abstractmethod
    def build_password(self):
        pass

    @abstractmethod
    def build_port(self):
        pass

    @abstractmethod
    def use_db(self):
        pass


class MysqlBuilder(Builder):
    def __init__(self,configfile,configname):
        self.sqlmoudle = sqlmoudle()
        self.configfile = configfile
        self.configname = configname
        self.config = ConfigParser.ConfigParser()
        self.config.readfp(open(configfile, 'rb'))



    def build_port(self):
        self.sqlmoudle.port = int(self.config.get(self.configname, 'port'))

    def build_server_address(self):
        self.sqlmoudle.host = self.config.get(self.configname, 'host')

    def build_password(self):
        self.sqlmoudle.passwd = self.config.get(self.configname, 'passwd')

    def use_db(self):
        self.sqlmoudle.db = self.config.get(self.configname, 'db')

    def build_username(self):
        self.sqlmoudle.user = self.config.get(self.configname, 'user')

    def get_moudle(self):
        return self.sqlmoudle

    def build_all(self):
        self.build_password()
        self.build_port()
        self.build_server_address()
        self.build_password()
        self.build_username()
        return self

    def get_tuples(self):
        return self.sqlmoudle.host, self.sqlmoudle.user, self.sqlmoudle.passwd, self.sqlmoudle.port
